package cnsatmsmart.automation.mmi.view.layers;

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.Layer;
import com.bbn.openmap.MapBean;
import com.bbn.openmap.event.ProjectionEvent;
import com.bbn.openmap.omGraphics.OMCircle;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.omGraphics.OMLine;
import com.bbn.openmap.proj.Length;
import com.bbn.openmap.proj.Projection;
import java.awt.Color;

import cnsatmsmart.automation.mmi.helper.utilities.GlobalEnvVars;

/**
 * @author SIK - PTIK, BPPT
 **/

public class RadarLayer extends Layer {
    /* const */
        private static final Color FIXES_COLOR = new Color (128, 225, 128); //Color.GRAY
            //nilai warna default untuk pewarnaan pada penggambaran radar yang merupakan instance dari kelas RadarLayer

    /* atribut - atribut yang dimiliki oleh kelas RadarLayer */
        private float latitude;     //posisi titik pusat radar pada garis lintang
        private float longitude;    //posisi titik pusat radar pada garis bujur
        private Color color;        //warna dari object radar
        
        private OMGraphicList graphics = new OMGraphicList();
        private Projection projection = null;
        private OMGraphic selectedGraphic;
        private MapBean map;

        private boolean isCreated = false;

    /* constructor - user defined ctor, no default ctor */
        // <editor-fold defaultstate="collapsed" desc="ctor">
        /*
         * @param = atribut
         */
        public RadarLayer (float lat, float lon, MapBean map) {
            this.latitude = lat;
            this.longitude = lon;
            this.color = FIXES_COLOR;
            this.map = map;
        }
        public RadarLayer (MapBean map, int latDegrees, int latMinutes, float latSeconds, char latSign, int lonDegrees, int lonMinutes, float lonSeconds, char lonSign) {
            this(0.0f, 0.0f, map);

            float radarCenterLat = this.latitude;
            float radarCenterLon = this.longitude;

            try {
                radarCenterLat = (latSign == 'N' ? 1.0f : -1.0f) * (latDegrees + (latMinutes / 60.0f) + (latSeconds / 3600.0f));
                radarCenterLon = (lonSign == 'E' ? 1.0f : -1.0f) * (lonDegrees + (lonMinutes / 60.0f) + (lonSeconds / 3600.0f));
            } catch (NumberFormatException nfexcpt) {
                radarCenterLat = this.latitude;
                radarCenterLon = this.longitude;
            } catch (Exception e) {
                radarCenterLat = this.latitude;
                radarCenterLon = this.longitude;
            }

            this.latitude = radarCenterLat;
            this.longitude = radarCenterLon;
        }
        // </editor-fold>

    /* setter */
        public void setCenter (float lat, float lon) {
            /* I.S. sebarang */
            /* F.S. this.latitude terdefinisi dan bernilai sama dengan parameter lat DAN this.longitude terdefinisi dan bernilai sama dengan parameter lon */
            /* Proses : mengisikan dan mengubah nilai this.latitude DAN this.longitude dengan parameter lat DAN lon */
                this.latitude = lat;
                this.longitude = lon;
        }

    /* getter */
        public LatLonPoint getCenter () {
            /* mengembalikan nilai dari atribut latitude dan longitude */
            return (new LatLonPoint(this.latitude, this.longitude));
        }
    
    /* method - method yang dimiliki oleh kelas RadarLayer */
        private OMGraphicList drawCenter (float lat, float lon) {
            /* pusat lingkaran digambarkan dalam representasi + */
            //calculate center
                OMGraphicList omGs = new OMGraphicList();

            OMLine line1 = new OMLine(lat, lon, -3, 0, 3, 0);
            line1.setLinePaint(this.color);
            OMLine line2 = new OMLine(lat, lon, 0, -3, 0, 3);
            line2.setLinePaint(this.color);

            omGs.add(line1);
            omGs.add(line2);

            return (omGs);
        }
        private OMCircle drawCircle (float lat, float lon, float nm) {
            //calculate center
                OMCircle circle = new OMCircle(lat, lon, nm, Length.NM);

            circle.setLinePaint(this.color);
        
            return (circle);
        }

        /* menggambar garis pembagi lingkaran radar dengan unit satuan pembagi 0.075 rad */
        private OMGraphicList drawBAngle (float lat, float lon, float rad) {
            float factor = 0.075f;  //radian

            OMGraphicList omGs = new OMGraphicList();

            //0 degree
                OMLine line = new OMLine(lat + rad, lon, lat + (rad + factor), lon, OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //180 degree
                line = new OMLine(lat - rad, lon, lat - (rad + factor), lon, OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //90 degree
                line = new OMLine(lat, lon + rad, lat, lon  + (rad + factor), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //270 degree
                line = new OMLine(lat, lon - rad, lat, lon  - (rad + factor), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            float xChange = (GlobalEnvVars.COS45 * rad);
            float yChange = (GlobalEnvVars.SIN45 * rad);

            //45 degree (may not correct)
                line = new OMLine(lat + xChange, lon + yChange, lat + (xChange + factor), lon  + (yChange + factor), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //135 degree (may not correct)
                line = new OMLine(lat - xChange, lon + yChange, lat - (xChange + factor), lon  + (yChange + factor), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //225 degree (may not correct)
                line = new OMLine(lat - xChange, lon - yChange, lat - (xChange + factor), lon  - (yChange + factor), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //315 degree (may not correct)
                line = new OMLine(lat + xChange, lon - yChange, lat + (xChange + factor), lon  - (yChange + factor), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            return (omGs);
        }
        private OMGraphicList drawSAngle (float lat, float lon, float rad) {
            float factor = 0.0375f;  //radian

            OMGraphicList omGs = new OMGraphicList();

            float xChange = (GlobalEnvVars.COS15 * rad);
            float yChange = (GlobalEnvVars.SIN15 * rad);

            float fx = (GlobalEnvVars.COS15 * factor);
            float fy = (GlobalEnvVars.SIN15 * factor);

            //15 degree (may not correct)
                OMLine line = new OMLine(lat + xChange, lon + yChange, lat + (xChange + fx), lon  + (yChange + fy), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);
                line = new OMLine(lat - xChange, lon + yChange, lat - (xChange + fx), lon  + (yChange + fy), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);
                line = new OMLine(lat - xChange, lon - yChange, lat - (xChange + fx), lon  - (yChange + fy), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);
                line = new OMLine(lat + xChange, lon - yChange, lat + (xChange + fx), lon  - (yChange + fy), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //30 degree (may not correct)
                line = new OMLine(lat + xChange, lon + yChange, lat + (xChange + fx), lon  + (yChange + fy), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //225 degree (may not correct)
                line = new OMLine(lat - xChange, lon - yChange, lat - (xChange + factor), lon  - (yChange + factor), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            //315 degree (may not correct)
                line = new OMLine(lat + xChange, lon - yChange, lat + (xChange + factor), lon  - (yChange + factor), OMGraphic.LINETYPE_STRAIGHT);
                line.setLinePaint(this.color);
                omGs.add(line);

            return (omGs);
        }

        
        /*
        
        //30 degree (this may not correct)
        xChange = (COS30 * radius);
        yChange = (SIN30 * radius);
        fX = (COS30 * factor);
        fY = (SIN30 * factor);
        line = new OMLine(lat + xChange, lon + yChange, lat + (xChange + fX), lon + (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat - xChange, lon + yChange, lat - (xChange + fX), lon + (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat - xChange, lon - yChange, lat - (xChange + fX), lon - (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat + xChange, lon - yChange, lat + (xChange + fX), lon - (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        //60 degree (this may not correct)
       xChange = (COS60 * radius);
        yChange = (SIN60 * radius);
        fX = (COS60 * factor);
        fY = (SIN60 * factor);
        line = new OMLine(lat + xChange, lon + yChange, lat + (xChange + fX), lon + (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat - xChange, lon + yChange, lat - (xChange + fX), lon + (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat - xChange, lon - yChange, lat - (xChange + fX), lon - (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat + xChange, lon - yChange, lat + (xChange + fX), lon - (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);
        //75 degree (this may not correct)
       xChange = (COS75 * radius);
        yChange = (SIN75 * radius);
        fX = (COS75 * factor);
        fY = (SIN75 * factor);
        line = new OMLine(lat + xChange, lon + yChange, lat + (xChange + fX), lon + (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat - xChange, lon + yChange, lat - (xChange + fX), lon + (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat - xChange, lon - yChange, lat - (xChange + fX), lon - (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);

        line = new OMLine(lat + xChange, lon - yChange, lat + (xChange + fX), lon - (yChange + fY),
                OMGraphic.LINETYPE_STRAIGHT);
        line.setLinePaint(color);
        omg.add(line);
        return omg;
    }
         */

    public void projectionChanged(ProjectionEvent pe) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    //

    /*

import com.bbn.openmap.omGraphics.OMTextLabeler;
import com.bbn.openmap.proj.Length;
import java.awt.Graphics;
import java.awt.Point;
     *
     *
     * //Enable/disable showing item
    boolean bDrawCircleCenter = false;
    boolean bDrawCircle = false;
    boolean bDrawCircleLabel = false;
    boolean bDrawBigAngle = false;
    boolean bDrawBigAngleLabel = false;
    boolean bDrawSmallAngle = false;
    boolean bDrawSmallAngleLabel = false;
     */

}
